package clue

import (
	"context"
	"encoding/json"
	"errors"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/infrastructure/common/constants"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/infrastructure/dto/base"
	pb "git.mycaigou.com/gfyx/micro-gfyx-oppty-service/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/clue"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/service/clue/customer"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/service/job_center"
	"git.myscrm.cn/golang/stark/v4"
)

type CustomerManageServer struct {
	customerService customer.CustomerManageServiceIface
	jobService      job_center.JobCenterServiceIface
}

func NewCustomerManageServer(
	customerService customer.CustomerManageServiceIface,
	jobService job_center.JobCenterServiceIface,
) pb.CustomerManageServiceServer {
	return &CustomerManageServer{
		customerService: customerService,
		jobService:      jobService,
	}
}

func (c *CustomerManageServer) MyResponsibleCustomers(ctx context.Context, request *pb.CustomerListRequest) (*pb.CustomerListResponse, error) {
	res, err := c.customerService.CustomerList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc CustomerManageServer MyResponsibleCustomers request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

func (c *CustomerManageServer) CustomerList(ctx context.Context, request *pb.CustomerListRequest) (*pb.CustomerListResponse, error) {
	res, err := c.customerService.CustomerList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc CustomerManageServer CustomerList request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

func (c *CustomerManageServer) ExportCustomerList(ctx context.Context, request *pb.CustomerListRequest) (*pb.ExportCustomerResponse, error) {
	var resp pb.ExportCustomerResponse

	request.Page, request.PageSize = 1, 1
	listRes, err := c.customerService.CustomerList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc CustomerManageServer ExportCustomerList request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	if listRes.Total == 0 {
		return nil, errors.New("导出数据为空")
	}

	requestBytes, _ := json.Marshal(request)
	jobId, err := c.jobService.NewJob(ctx, constants.JobMethodExport, constants.JobTypeCustomerExport, int32(listRes.Total), string(requestBytes))
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc CustomerManageServer ExportCustomerList request:%+v, err:%s", request, err.Error())
		return nil, err
	}

	_ = c.jobService.StartJob(ctx, jobId)
	resp.JobId = jobId
	return &resp, nil
}

func (c *CustomerManageServer) CustomerGroupOption(ctx context.Context, request *pb.CustomerGroupOptionRequest) (*pb.CustomerGroupOptionResponse, error) {
	res, err := c.customerService.CustomerGroupOption(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc CustomerManageServer CustomerGroupOption request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

func (c *CustomerManageServer) SaveCustomer(ctx context.Context, request *pb.SaveCustomerRequest) (*pb.SaveCustomerResponse, error) {
	res, err := c.customerService.SaveCustomer(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc CustomerManageServer Save request:%+v, err:%s", request, err.Error())
		return nil, err
	}

	return res, nil
}

func (c *CustomerManageServer) DeleteCustomer(ctx context.Context, request *pb.DeleteCustomerRequest) (*pb.DeleteCustomerResponse, error) {
	res, err := c.customerService.DeleteCustomer(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc CustomerManageServer Delete request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

func (c *CustomerManageServer) ImportCustomer(ctx context.Context, request *pb.ImportCustomerRequest) (*pb.ImportCustomerResponse, error) {
	// 读取文件内容，校验文件内容是否合理
	fileResponse, cacheKey, err := c.customerService.ImportFile(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc ImportFile request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	// 插入任务中心，启动任务
	importRequest := base.ImportRequest{
		FileId:   request.FileId,
		IsBatch:  false,
		CacheKey: cacheKey,
	}
	requestBytes, _ := json.Marshal(importRequest)

	jobId, err := c.jobService.NewJob(ctx, constants.JobMethodImport, constants.JobTypeCustomerImport, fileResponse.Total, string(requestBytes))
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc ImportCustomer NewJob request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	err = c.jobService.StartJob(ctx, jobId)
	if err != nil {
		return nil, err
	}
	fileResponse.JobId = jobId
	fileResponse.IsSucceed = true
	return fileResponse, nil
}

// ImportCustomerList 查询导入列表
func (c *CustomerManageServer) ImportCustomerList(ctx context.Context, request *pb.ImportCustomerListRequest) (*pb.ImportCustomerListResponse, error) {
	response, err := c.customerService.ImportCustomerList(ctx, request)
	if err != nil {
		return nil, err
	}
	return response, nil
}

func (c *CustomerManageServer) ImportCustomerStatus(ctx context.Context, request *pb.ImportCustomerStatusRequest) (*pb.ImportCustomerStatusResponse, error) {
	response, err := c.customerService.ImportCustomerStatus(ctx, request)
	if err != nil {
		return nil, err
	}
	return response, nil
}

func (c *CustomerManageServer) ImportCustomerEdit(ctx context.Context, request *pb.ImportCustomerEditRequest) (*pb.ImportCustomerEditResponse, error) {
	err := c.customerService.ImportCustomerEdit(ctx, request)
	if err != nil {
		return nil, err
	}
	return &pb.ImportCustomerEditResponse{}, nil
}

func (c *CustomerManageServer) ImportCustomerDelete(ctx context.Context, request *pb.ImportCustomerDeleteRequest) (*pb.ImportCustomerDeleteResponse, error) {
	err := c.customerService.ImportCustomerDelete(ctx, request)
	if err != nil {
		return nil, err
	}
	return &pb.ImportCustomerDeleteResponse{}, nil
}

func (c *CustomerManageServer) ImportCustomerCount(ctx context.Context, request *pb.ImportCustomerCountRequest) (*pb.ImportCustomerCountResponse, error) {
	response, err := c.customerService.ImportCustomerCount(ctx, request)
	if err != nil {
		return nil, err
	}
	return response, nil
}
